package
{
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.geom.Point;
	
	import mx.containers.Canvas;
	import mx.events.DragEvent;

	public class SlideCanvas extends Canvas
	{
		private var ul:Mover;
		private var br:Mover;
		
		private var _currentObject:MovableComponent;

		public function SlideCanvas()
		{
			super();
		}
		
		override protected function createChildren():void{
			ul = new Mover();
			ul.visible = false;
			ul.addEventListener("dragging", onBRMove);
			addChild(ul);
			
			br = new Mover();
			br.visible = false;
			br.addEventListener("dragging", onBRMove);
			addChild(br);
			
			super.createChildren();
		}
		
		private function onBRMove(e:Event):void{
			updateSurface();
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
//			var g:Graphics = graphics;
//			g.clear();
//			g.lineStyle(1);
//			g.moveTo(0, height/2);
//			g.lineTo(width, height/2);
//			g.moveTo(width/2, 0);
//			g.lineTo(width/2, height);
//			g.endFill();
			
			super.updateDisplayList(unscaledWidth, unscaledHeight);
		}
		
		private function updateSurface():void{
			//_currentObject.width = _currentObject.height = undefined;
			
			var ulGP:Point = ul.localToGlobal(new Point(0,0));
			var brGP:Point = br.localToGlobal(new Point(0,0));
			var ulSlideP:Point = yshow.slide.globalToLocal(ulGP);
			var brSlideP:Point = yshow.slide.globalToLocal(brGP);
			
			_currentObject.x = ul.x;
			_currentObject.y = ul.y;
			_currentObject.width = brSlideP.x - ulSlideP.x + br.width;
			_currentObject.height = brSlideP.y - ulSlideP.y + br.height;
			
//			_currentObject.setStyle("left", ulSlideP.x);
//			_currentObject.setStyle("top", ulSlideP.y);
//			_currentObject.setStyle("right", yshow.slide.width - brSlideP.x - br.width);
//			_currentObject.setStyle("bottom", yshow.slide.height - brSlideP.y - br.height);
			
			//ul.x = ul.y = 0;
			
			//trace(ulSlideP.x, ulSlideP.y);
		}
		
		private function updateMovers():void{
 			ul.x = _currentObject.x;
			ul.y = _currentObject.y;
			
			br.x = _currentObject.x + _currentObject.width - br.width;
			br.y = _currentObject.y + _currentObject.height - br.height;
		}
		
		override public function addChild(child:DisplayObject):DisplayObject{
			if(child is MovableComponent){
				child.addEventListener(Event.SELECT, onObjectSelect);
				child.addEventListener(DragEvent.DRAG_START, onObjectDragStart);
				child.addEventListener(DragEvent.DRAG_COMPLETE, onObjectDragComplete);
			}
			return super.addChild(child);
		}
		
		private function onObjectDragStart(e:Event):void{
			ul.visible = br.visible = false;
		}
		
		private function onObjectDragComplete(e:Event):void{
			ul.visible = br.visible = true;
			updateMovers();
		}
		
		private function onObjectSelect(e:Event):void{
			if(e.currentTarget != _currentObject){
				_currentObject = e.currentTarget as MovableComponent;
				_currentObject.width = _currentObject.height = 50;
				
				bringToFront(ul);
				bringToFront(br);
				
				updateMovers();
				
				ul.visible = br.visible = true;
			}
		}
		
		private function bringToFront(child:DisplayObject):void{
			super.addChild(super.removeChild(child));
		}
	}
}